let layout = [
    {name: '名称', treeNodes: true, headerClass: 'value_col', colClass: 'value_col', style: ''},
    {
        name: '类型', headerClass: 'value_col', colClass: 'value_col', style: 'width: 20%',
        render: function (row) {
            return row.type == 0?'菜单':'按钮';
        }
    },
    {
        name: '操作',
        headerClass: 'value_col',
        colClass: 'value_col',
        style: 'width: 20%',
        render: function (row) {
            let id = "'"+row.id +"'";
            let url = "'"+ '/sys/resource/del' + "'";
            return '<a class="layui-btn layui-btn-warm layui-btn-sm" onclick="edit(' + id + ')"><i class="layui-icon">&#xe642;</i> 编辑</a>'+
                   '<a class="layui-btn layui-btn-danger layui-btn-sm" onclick="del(' + id + ', '+url+')"><i class="layui-icon">&#xe640;</i> 删除</a>'; //列渲染
        }
    }
];
layui.config({
    base: '/static/layui/'
}).extend({
    index: 'index'
}).use(['form', 'tree', 'layer'], function () {
    let layer = layui.layer, form = layui.form, $ = layui.jquery, n = [],tree;
        $.ajax({
            sync: true,
            type: 'GET',
            dataType: 'json',
            url: '/sys/resource/getResourceTree',
            success: function (res) {
                n = res.data;
                tree = layui.treeGird({
                    elem: '#treeTable', //传入元素选择器
                    spreadable: false, //设置是否全展开，默认不展开
                    checkbox: true,
                    nodes: n,
                    layout: layout
                });
                form.render();
                form.on('checkbox(*)', function (data) {
                    console.log("a：" + data.elem);
                    console.log("b: " + data.elem.checked)
                    console.log("c: " + data.value)
                    console.log("d: " + data.othis)
                    let arr = layui.getSelected(tree);
                    console.log("e: " + arr.length)
                });
            }
        });
    $("#add").on('click', function () {
        openWindow('700px', '450px', '添加资源', '/sys/resource/formView');
    });
    $("#del").on('click', function () {
       let arr = layui.getSelected(tree);
       let id = $.map(arr,function (row) {
          return row.id;
       });
      id = id.join(",");
      del(id, "/sys/resource/del");
    });
});

function edit(id) {
    openWindow('700px', '450px', '编辑资源', '/sys/resource/formView?id=' + id);
}